package cn.xaut.数组;

public class demo11 {

    static int getArea(int i, int j, int a, int b) {
        return Math.abs(i - j) * Math.min(a, b);
    }

    // 双指针（对撞指针）
    // 时间复杂度：O(n)
    public int maxArea(int[] height) {

        int l = 0, r = height.length - 1;
        int res = 0;
        int w = r - l;
        while (r > l) {
            int h = Math.min(height[l], height[r]);
            res = Math.max(res, w * h);
            if (height[l] < height[r]) 
                l++;
            else 
                r--;
            w--;
        }
        
        return res;
    }

    // 两层循环
    // 时间复杂度：O(n^2)
    public int maxArea2(int[] height) {

        int len = height.length;
        if (len < 2)
            return 0;

        int res = 0;
        for (int i = 0; i < len; i++)
            for (int j = i + 1; j < len; j++)
                res = Math.max(res, getArea(i, j, height[i], height[j]));

        return res;
    }

    public static void main(String[] args) {

        System.out.println(new demo11().maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));//49
        System.out.println(new demo11().maxArea(new int[]{1, 1}));//1
        System.out.println(new demo11().maxArea(new int[]{4, 3, 2, 1, 4}));//16
        System.out.println(new demo11().maxArea(new int[]{1, 2, 1}));//2
    }
}
